package com.youhome.service.service.impl;


import com.youhome.service.service.TranslationService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import sun.misc.IOUtils;

import java.io.IOException;
import java.util.Locale;

/**
 * Created by chen365 on 2018/08/30.
 */
@Service
public class TranslationServiceImpl implements TranslationService {

    @Value("classpath:static/json/details_values_translation.json")
    private Resource translationResource;

    @Value("classpath:static/json/details_values_translation_en.json")
    private Resource translationResourceEn;

    @Override
    public String getTranslationDictJson() {
        String json = null;
        try {
            Locale locale = LocaleContextHolder.getLocale();

            System.err.println("Locale:"+Locale.getDefault().getLanguage());
            if (locale.equals(Locale.SIMPLIFIED_CHINESE)) {
                json = new String(IOUtils.readFully(translationResource.getInputStream(), -1, true));
            } else if (locale.equals(Locale.US)) {
                json = new String(IOUtils.readFully(translationResourceEn.getInputStream(), -1, true));
            } else {
                json = new String(IOUtils.readFully(translationResource.getInputStream(), -1, true));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }
}
